//
//  WDArrivalServiceItemViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/4.
//

#import "WDArrivalServiceItemViewController.h"
#import "WDArrivalServiceItemViewModel.h"
#import "WDArrivalServiceItemFirstViewCell.h"
#import "WDArrivalServiceItemViewCell.h"
#import "WDArrivalServiceHeaderView.h"

@interface WDArrivalServiceItemViewController ()
@property (nonatomic, strong) WDArrivalServiceItemViewModel *viewModel;
@end

/// <#Description#>
@implementation WDArrivalServiceItemViewController

- (void)dealloc
{
    debugMethod();
}

- (void)initWithVar
{
    [super initWithVar];
    
    // 登录成功，发送通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccessNoti:) name:kUSER_LOGIN_SUC_NOTI object:nil];
}

- (void)initWithViewModel
{
    _viewModel = [WDArrivalServiceItemViewModel new];
    _viewModel.typeName = self.typeName;
    _viewModel.reqModel.servFlow = @(self.servFlow);
    [_viewModel setupData];
}

- (void)initWithNavBar
{
    [super initWithNavBar];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scanCodeSuccessNoti:) name:kARRIVAL_SCAN_CODE_SUCCESS_NOTI object:nil];
}

- (void)initWithSubView
{
    [super initWithSubView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.baseTableView.backgroundColor = [UIColor whiteColor];
    
    [self.baseTableView registerClass:[WDArrivalServiceItemFirstViewCell class] forCellReuseIdentifier:[WDArrivalServiceItemFirstViewCell className]];
    [self.baseTableView registerClass:[WDArrivalServiceItemViewCell class] forCellReuseIdentifier:[WDArrivalServiceItemViewCell className]];
    self.baseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self hiddenFooterView:YES];
    
    [self doRefreshData];
}

/// 扫码成功
/// - Parameter noti: noti description
- (void)scanCodeSuccessNoti:(NSNotification *)noti {
    WDArrivalServiceItemModel *model = [noti object];
    if (model && model.id > 0 && model.taskType == 0) {
        [self doRefreshData];
        [self handleServiceItem:model];
    }
}

/// 登录成功，收到通知
/// - Parameter noti: noti description
- (void)loginSuccessNoti:(NSNotification *)noti {
    [self doRefreshData];
}

- (void)doRefreshData
{
    [super doRefreshData];
    self.viewModel.pageId = 1;
    [self fromServerRequestData];
}

- (void)doMoreRefreshData {
    [super doMoreRefreshData];
    
    [self fromServerRequestData];
}

- (void)fromServerRequestData {
    
    @weakify(self);
    [self.viewModel requestWithArrivalService:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [self.baseTableView reloadData];
        }
        [self endAllRefreshing];
        
        if (self.viewModel.dataList.count < 10) {
            [self hiddenFooterView:YES];
        } else {
            [self hiddenFooterView:NO];
        }
    }];
     
}

- (void)handleServiceItem:(WDArrivalServiceItemModel *)model {
    @weakify(self);
    void(^block)(id data) = ^(id data) {
        @strongify(self);
        [self doRefreshData];
    };
    
    NSDictionary *params = @{
        @"serviceModel": model,
        @"block":block
    };
    
    UIViewController *vc = nil;
    // 服务流程 0.待分派 1.待接车 2.检查中 3.维修中 4.待报单 5.已完成 6.已取消 7.已关闭
    if (model.servFlow == 1) { // 待接车
        vc = [[TYZRouter sharedInstance] getServiceOrderWaitVehicleVC:params];
    } else if (model.servFlow == 2) { // 检查中
        vc = [[TYZRouter sharedInstance] getServiceOrderReviewVehicleVC: params];
    } else if (model.servFlow == 3) { // 维修中
        vc = [[TYZRouter sharedInstance] getServiceOrderMaintenanceVehicleVC:params];
    } else if (model.servFlow == 4) { // 待报单
        vc = [[TYZRouter sharedInstance] getServiceOrderPendingVehicleVC:params];
    }
    
    if (vc) {
        [TYZJumpVCManager pushBaseVC:self pushVC:vc];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.dataList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return kWDArrivalServiceItemFirstViewCellHeight;
    }
    return kWDArrivalServiceItemViewCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WDArrivalServiceItemModel *model = [self.viewModel.dataList objectOrNilAtIndex:indexPath.row];
    if (indexPath.row == 0) {
        WDArrivalServiceItemFirstViewCell *cell = [WDArrivalServiceItemFirstViewCell cellForTableView:tableView indexPath:indexPath];
        [cell updateCellData:model];
        return cell;
    }
    WDArrivalServiceItemViewCell *cell = [WDArrivalServiceItemViewCell cellForTableView:tableView indexPath:indexPath];
    [cell updateCellData:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WDArrivalServiceItemModel *model = [self.viewModel.dataList objectOrNilAtIndex:indexPath.row];
    
//    NSString *vin = [model.vehicleVin substringFromIndex:model.vehicleVin.length - 8];
//    debugLog(@"all=%@; vin=%@; vin.len=%zd", model.vehicleVin, vin, vin.length);
//    return;
    
    [self handleServiceItem: model];
}


@end
